package com.ssyt.examinationsystem.controller;

import com.alibaba.fastjson.JSON;
import com.ssyt.examinationsystem.entity.RuleInfo;
import com.ssyt.examinationsystem.service.IRuleInfoService;
import com.ssyt.examinationsystem.service.impl.RuleInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * Servlet implementation class RuleInfoController
 */
public class RuleInfoController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RuleInfoController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 调用规则表的业务对象
        IRuleInfoService ruleService = new RuleInfoServiceImpl();
        // 获取输出流对象
        PrintWriter out = response.getWriter();
        // 获取地址栏上的flag值
        String flag = request.getParameter("flag");
        if ("list".equals(flag)) {
            list(request, response);
        } else if ("query".equals(flag)) {
            query(ruleService, request, response, out);
        } else if ("update".equals(flag)) {
            update(ruleService, request, response, out);
        } else if ("dele".equals(flag)) {
            delete(ruleService, request, response, out);
        } else if ("add".equals(flag)) {
            add(ruleService, request, response, out);
        } else if ("initAdd".equals(flag)) {
            initAdd(request, response);
        }

        out.flush();
        out.close();
    }

    /**
     * 转发到添加页面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void initAdd(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 转发
        request.getRequestDispatcher("WEB-INF/views/rule/ruleAdd.jsp").forward(request, response);
    }

    /**
     * 添加规则信息的方法
     *
     * @param ruleService
     * @param request
     * @param response
     * @param out
     */
    private void add(IRuleInfoService ruleService, HttpServletRequest request, HttpServletResponse response,
                     PrintWriter out) {
        // 获取表单数据
        Integer scNumber = Integer
                .valueOf("".equals(request.getParameter("scNumber")) ? "0" : request.getParameter("scNumber"));
        Float scScore = Float
                .valueOf("".equals(request.getParameter("scScore")) ? "0" : request.getParameter("scScore"));
        String scIds = request.getParameter("scIds");

        Integer mulNumber = Integer
                .valueOf("".equals(request.getParameter("mulNumber")) ? "0" : request.getParameter("mulNumber"));
        Float mulScore = Float
                .valueOf("".equals(request.getParameter("mulScore")) ? "0" : request.getParameter("mulScore"));
        String mulIds = request.getParameter("mulIds");

        Integer bkNumber = Integer
                .valueOf("".equals(request.getParameter("bkNumber")) ? "0" : request.getParameter("bkNumber"));
        Float bkScore = Float
                .valueOf("".equals(request.getParameter("bkScore")) ? "0" : request.getParameter("bkScore"));
        String bkIds = request.getParameter("bkIds");

        Integer jdNumber = Integer
                .valueOf("".equals(request.getParameter("jdNumber")) ? "0" : request.getParameter("jdNumber"));
        Float jdScore = Float
                .valueOf("".equals(request.getParameter("jdScore")) ? "0" : request.getParameter("jdScore"));
        String jdIds = request.getParameter("jdIds");

        Integer subNumber = Integer
                .valueOf("".equals(request.getParameter("subNumber")) ? "0" : request.getParameter("subNumber"));
        Float subScore = Float
                .valueOf("".equals(request.getParameter("subScore")) ? "0" : request.getParameter("subScore"));
        String subIds = request.getParameter("subIds");

        Integer testType = Integer
                .valueOf("".equals(request.getParameter("testType")) ? "0" : request.getParameter("testType"));
        Float totalScore = Float
                .valueOf("".equals(request.getParameter("totalScore")) ? "0" : request.getParameter("totalScore"));
        // 创建一个规则对象，封装属性
        RuleInfo rule = new RuleInfo();
        rule.setScNumber(scNumber);
        rule.setScScore(scScore);
        rule.setScIds(scIds);

        rule.setMulNumber(mulNumber);
        rule.setMulScore(mulScore);
        rule.setMulIds(mulIds);
        rule.setBkNumber(bkNumber);
        rule.setBkScore(bkScore);
        rule.setBkIds(bkIds);
        rule.setJdNumber(jdNumber);
        rule.setJdScore(jdScore);
        rule.setJdIds(jdIds);
        rule.setSubNumber(subNumber);
        rule.setSubScore(subScore);
        rule.setSubIds(subIds);
        rule.setTotalScore(totalScore);
        rule.setTestType(testType);
        // 调用添加的业务方法
        int row = ruleService.addRule(rule);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 删除规则信息的方法
     *
     * @param ruleService
     * @param request
     * @param response
     * @param out
     */
    private void delete(IRuleInfoService ruleService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) {
        // 获取地址栏上的规则编号
        String ruleId = request.getParameter("ruleId");
        // 调用根据规则编号删除规则的方法
        int row = ruleService.deleteRule(ruleId);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 修改规则信息的方法
     *
     * @param ruleService
     * @param request
     * @param response
     * @param out
     */
    private void update(IRuleInfoService ruleService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) {
        // 获取地址栏上的规则编号
        Integer ruleId = Integer.valueOf(request.getParameter("ruleId"));
        // 获取表单数据
        Integer scNumber = Integer
                .valueOf("".equals(request.getParameter("scNumber")) ? "0" : request.getParameter("scNumber"));
        Float scScore = Float
                .valueOf("".equals(request.getParameter("scScore")) ? "0" : request.getParameter("scScore"));
        String scIds = request.getParameter("scIds");

        Integer mulNumber = Integer
                .valueOf("".equals(request.getParameter("mulNumber")) ? "0" : request.getParameter("mulNumber"));
        Float mulScore = Float
                .valueOf("".equals(request.getParameter("mulScore")) ? "0" : request.getParameter("mulScore"));
        String mulIds = request.getParameter("mulIds");

        Integer bkNumber = Integer
                .valueOf("".equals(request.getParameter("bkNumber")) ? "0" : request.getParameter("bkNumber"));
        Float bkScore = Float
                .valueOf("".equals(request.getParameter("bkScore")) ? "0" : request.getParameter("bkScore"));
        String bkIds = request.getParameter("bkIds");

        Integer jdNumber = Integer
                .valueOf("".equals(request.getParameter("jdNumber")) ? "0" : request.getParameter("jdNumber"));
        Float jdScore = Float
                .valueOf("".equals(request.getParameter("jdScore")) ? "0" : request.getParameter("jdScore"));
        String jdIds = request.getParameter("jdIds");

        Integer subNumber = Integer
                .valueOf("".equals(request.getParameter("subNumber")) ? "0" : request.getParameter("subNumber"));
        Float subScore = Float
                .valueOf("".equals(request.getParameter("subScore")) ? "0" : request.getParameter("subScore"));
        String subIds = request.getParameter("subIds");

        Integer testType = Integer
                .valueOf("".equals(request.getParameter("testType")) ? "0" : request.getParameter("testType"));
        Float totalScore = Float
                .valueOf("".equals(request.getParameter("totalScore")) ? "0" : request.getParameter("totalScore"));
        // 创建一个规则对象，封装属性
        RuleInfo rule = new RuleInfo();
        rule.setScNumber(scNumber);
        rule.setScScore(scScore);
        rule.setScIds(scIds);
        rule.setRuleId(ruleId);
        rule.setMulNumber(mulNumber);
        rule.setMulScore(mulScore);
        rule.setMulIds(mulIds);
        rule.setBkNumber(bkNumber);
        rule.setBkScore(bkScore);
        rule.setBkIds(bkIds);
        rule.setJdNumber(jdNumber);
        rule.setJdScore(jdScore);
        rule.setJdIds(jdIds);
        rule.setSubNumber(subNumber);
        rule.setSubScore(subScore);
        rule.setSubIds(subIds);
        rule.setTotalScore(totalScore);
        rule.setTestType(testType);
        // 调用修改规则信息的业务方法
        int row = ruleService.updateRule(rule);
        if (row > 0) {
            out.print("ok");
        }

    }

    private void query(IRuleInfoService ruleService, HttpServletRequest request, HttpServletResponse response,
                       PrintWriter out) {
        // 调用查询所有规则信息的业务方法
        List<Map<String, Object>> list = ruleService.getRules();
        // 将集合转化为json
        String json = JSON.toJSONString(list, true);
        out.print(json);
    }

    /**
     * 显示所有规则信息
     *
     * @param ruleService
     * @param request
     * @param response
     * @param out
     * @throws IOException
     * @throws ServletException
     */
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 转发
        request.getRequestDispatcher("WEB-INF/views/rule/ruleList.jsp").forward(request, response);
    }

}
